import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;

public class DockerPluginFunctionalTest {
    @TempDir
    File projectDir;

    private File getBuildFile() {
        return new File(projectDir, "build.gradle");
    }

    private File getSettingsFile() {
        return new File(projectDir, "settings.gradle");
    }

    @Test
    void canRunTask() throws IOException {
        System.out.println(checkDockerAvailability());
        // Run the build
        GradleRunner runner = GradleRunner.create();
        runner.withPluginClasspath();
        //runner.withArguments("tasks");
        runner.withArguments("bootJar");
        //runner.withArguments("build");
        runner.withProjectDir(Path.of("../../demo").toFile());
        var result = runner.build();
        System.out.println(result.getOutput());
        // Verify the result
        //assertTrue(result.getOutput().contains("Hello from plugin 'org.example.greeting'"));
    }

    private void writeString(File file, String string) throws IOException {
        try (Writer writer = new FileWriter(file)) {
            writer.write(string);
        }
    }

    private boolean checkDockerAvailability() {
        try {
            Process process = new ProcessBuilder("docker", "version").start();
            return process.waitFor() == 0;
        } catch (Exception e) {
            return false;
        }
    }

}
